//
//  BSVoiceCell.m
//  BS
//
//  Created by lanou on 15/8/18.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "BSVoiceCell.h"

#import "UIImageView+WebCache.h"

#import "PlayerManger.h"

#import "Header.h"

#import "UILabel+LJLabel.h"

#import "PlayerManger.h"
@implementation BSVoiceCell

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"开始播放" object:nil];
}

- (void)awakeFromNib {
    // Initialization code
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)setModel:(BSListModel *)model{
    
    _model = model;
    _contentLabel.text = _model.text;
     _voiceTimeLabe.text = [NSString stringWithFormat:@"%02ld:%02ld", [_model.voicetime integerValue] / 60, [_model.voicetime integerValue] % 60];
    if(_model.playcount){
    _playCountLabel.text =[NSString stringWithFormat:@"%@播放",_model.playcount];
    }
    //自定义行间距
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:model.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5];//调整行间距
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [model.text length])];
    _contentLabel.attributedText = attributedString;
    [_contentLabel sizeToFit];
    [_imaView sd_setImageWithURL:[NSURL URLWithString:model.bimageuri]];
    [self changeSongView];
}



- (void)changeSongView{

    if(_model == [PlayerManger defaultManger].currentModel && [PlayerManger defaultManger].mediaType == Song){
        _songView.hidden = NO;
        _playButton.hidden = YES;
        _voiceTimeLabe.hidden = YES;
    }
    else{
        _songView.hidden = YES;
        _playButton.hidden = NO;
        _voiceTimeLabe.hidden = NO;

    }
}


+ (CGFloat)heightOfCell:(BSListModel *)model{
    
    if (model.cellHeight == 0){
        model.cellHeight = [UILabel heightOfLabelByStr:model.text fontSize:17 width:SCREEN_WIDTH - 2 * 8 lineSpacing:5] + 16 + (SCREEN_WIDTH / [model.width floatValue] * [model.height floatValue]);
    }
    return model.cellHeight;
}


- (IBAction)play:(UIButton *)sender {
    
    NSLog(@"播放声音");
    __weak PlayerManger *manager = [PlayerManger defaultManger];
    manager.currentModel = _model;
    manager.startBlock = ^{
    [manager.currentCell changeSongView];
    manager.currentCell = self;
    };
    manager.finishBlock = ^{
        _playButton.hidden = NO;
        _songView.hidden = YES;
        _voiceTimeLabe.hidden = NO;
    };
    [manager playWithURL:_model.voiceuri frame:_songView.bounds mediaType:Song];
    [self.songView addSubview:manager.songView];
    [self changeSongView];
}
@end
